package data.dblp;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

public class GeneratorData {

	/**
	 * @param args
	 */
	public static int total = 1312012;
	public static String folder = "/home/quyet/experiments/dataset/dblp/3200000";

	public static void main(String[] args) {
		//GetData(folder + "/part1-16.txt");
		Reorder();
	}

	// Reorder
	public static void Reorder() {
		try {
			FileReader fr = new FileReader(new File(folder + "/nodes.txt"));
			BufferedReader br = new BufferedReader(fr);

			FileWriter fEdge2 = new FileWriter(folder + "/edges2.txt");
			FileWriter fNode2 = new FileWriter(folder + "/nodes2.txt");

			BufferedWriter bwEdge = new BufferedWriter(fEdge2);
			BufferedWriter bwNode = new BufferedWriter(fNode2);

			String strLine;
			HashMap<String, Node> V = new HashMap<String, Node>();
			int id = 1;
			int line = 0;

			while ((strLine = br.readLine()) != null) {

				String[] row = strLine.split("\t");

				Node ni = new Node(id, row[0]);
				Node tmp = V.get(row[0]);
				if (tmp == null) {
					V.put(row[0], ni);
					id += 1;
				} else {
					ni = tmp;
				}
				String node = ni.id + "";

				for (int i = 1; i < 6; i++)
					node += "\t" + row[i];

				for (int i = 6; i < row.length; i++) {
					// Node nj = new Node(row[i]);
					Node m = V.get(row[i]);
					if (m == null) {
						V.put(row[i], new Node(id, row[i]));
						bwEdge.write(ni.id + "\t" + id + "\n");
						node += "\t" + id;
						id++;
					} else {
						bwEdge.write(ni.id + "\t" + m.id + "\n");
						node += "\t" + m.id;
					}
				}
				bwNode.write(node);
				bwNode.newLine();

				line += 1;
				System.out.println("Running " + (line) + "/" + total + " -> "
						+ (100 * (line) / total) + "%");
			}
			br.close();
			bwEdge.close();
			bwNode.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static void GetData(String filepath) {
		// TODO Auto-generated method stub

		try {
			FileReader fr = new FileReader(new File(filepath));
			BufferedReader br = new BufferedReader(fr);

			FileWriter fEdge = new FileWriter(folder + "/edges.txt");
			FileWriter fLabel = new FileWriter(folder + "/labels.txt");
			FileWriter fInfo = new FileWriter(folder + "/info.txt");
			BufferedWriter bwEdge = new BufferedWriter(fEdge);
			BufferedWriter bwLalel = new BufferedWriter(fLabel);
			BufferedWriter bwInfo = new BufferedWriter(fInfo);

			HashSet<String> listL = new HashSet<String>();

			String strLine;
			HashMap<String, Node> V = new HashMap<String, Node>();
			int countEdges = 0;
			int line = 0;

			while ((strLine = br.readLine()) != null) {

				String[] row = strLine.split("\t");
				// if (row.length >= 6) {

				if (!listL.contains(row[4])) {
					listL.add(row[4]);
					bwLalel.write(row[4] + "\n");
				}

				Node ni = new Node(Integer.parseInt(row[0]));
				V.put(row[0], new Node(Integer.parseInt(row[0])));

				for (int i = 6; i < row.length; i++) {
					Node m = V.get(row[i]);
					if (m == null) {
						V.put(row[i], new Node(Integer.parseInt(row[i])));
						bwEdge.write(ni.id + "\t" + row[i] + "\n");
					} else {
						bwEdge.write(ni.id + "\t" + m.id + "\n");
					}
					countEdges += 1;
				}
				// }
				line += 1;

				System.out.println("Running " + (V.size() + countEdges) + "/"
						+ total + " -> "
						+ (100 * (V.size() + countEdges) / total)
						+ "% -> line: " + line);
				if (V.size() + countEdges >= total)
					break;
			}

			bwInfo.write(V.size() + "\t" + countEdges + "\t"
					+ (V.size() + countEdges) + "\n");
			bwInfo.write("number of lables: " + listL.size() + "\n");
			bwInfo.write("line: " + line + " in file " + filepath);
			br.close();
			bwInfo.close();
			bwLalel.close();
			bwEdge.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

class Node {
	int id;
	String label;
	ArrayList<Integer> neighbors;

	public Node() {
		this.label = "";
	}

	public Node(int id) {
		this.id = id;
	}

	public Node(Integer id, String label) {
		this.id = id;
		this.label = label;
	}

	public Node(Integer id, ArrayList<Integer> neighbors) {
		this.id = id;
		this.neighbors = neighbors;
	}

	public Node(Integer id, String label, ArrayList<Integer> neighbors) {
		this.id = id;
		this.label = label;
		this.neighbors = neighbors;
	}

	@Override
	public boolean equals(Object obj) {
		boolean check = false;
		if (obj == null)
			return false;
		else if (obj == this)
			return true;
		else {
			if (obj instanceof Node && obj.getClass() == this.getClass()) {
				final Node t = (Node) obj;
				check = (this.id == t.id);
			}
		}
		return check;
	}

	@Override
	public int hashCode() {
		int hash = 7;
		hash = (37 * hash) + this.id;
		return hash;
	}
}